﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Bigeagle.Portable.BoardGames;
using Bigeagle.Portable.BoardGames.Go;
using Windows.Storage;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Popups;
using Windows.Storage.Streams;
using System.Diagnostics;

// “分组项页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234231 上提供

namespace EagleGO
{
    /// <summary>
    /// 显示分组的项集合的页。
    /// </summary>
    public sealed partial class MainPage : EagleGO.Common.LayoutAwarePage
    {
        /// <summary>
        /// 棋谱编码
        /// </summary>
        Encoding _KiFUEncoding = Encoding.GetEncoding("gb2312");

        ///// <summary>
        ///// 本页用到的棋谱分类
        ///// </summary>
        //private List<KiFUItem> _KiFUItems;

        /// <summary>
        /// 构造函数
        /// </summary>
        public MainPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// 使用在导航过程中传递的内容填充页。在从以前的会话
        /// 重新创建页时，也会提供任何已保存状态。
        /// </summary>
        /// <param name="navigationParameter">最初请求此页时传递给
        /// <see cref="Frame.Navigate(Type, Object)"/> 的参数值。
        /// </param>
        /// <param name="pageState">此页在以前会话期间保留的状态
        /// 字典。首次访问页面时为 null。</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            // TODO: 将可绑定组集合分配到 this.DefaultViewModel["Groups"]
            //_KiFUItems = navigationParameter as List<KiFUItem>;
            this.groupedItemsViewSource.Source = navigationParameter;
;        }

        protected override void SaveState(Dictionary<string, object> pageState)
        {
            base.SaveState(pageState);
        }

        /// <summary>
        /// 窗体载入事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pageRoot_Loaded(object sender, RoutedEventArgs e)
        {
            //try
            //{
            //    Uri uri = new Uri("http://weiqi.tom.com/qipu/201302/4jptfy-0212sgdqtw.sgf");
            //    KiFUItem item = new KiFUItem("common", uri);
            //    bool loaded = await item.LoadGameInfoAsync(_KiFUEncoding);
            //    if (loaded)
            //    {
            //        MessageDialog dialog = new MessageDialog(item.GameInfo.Event);
            //        await dialog.ShowAsync();
            //    }
            //    else
            //    {
            //        MessageDialog dialog = new MessageDialog("load failed");
            //        await dialog.ShowAsync();
            //    }
                
            //}
            //catch (Exception ex)
            //{
            //    MessageDialog dialog = new MessageDialog(ex.ToString());
            //    dialog.ShowAsync();
            //}
        }

        /// <summary>
        /// 项点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void itemGridView_ItemClick(object sender, ItemClickEventArgs e)
        {
            Frame rootFrame = Window.Current.Content as Frame;
            if (rootFrame != null)
            {
                if (!rootFrame.Navigate(typeof(KiFUPage), e.ClickedItem))
                {
                    throw new Exception("Failed to create initial page");
                }
            }
        }

        private void GroupTitle_Tapped(object sender, TappedRoutedEventArgs e)
        {
            Debug.WriteLine(sender);
        }
    }//end class

}//end namespace
